#! /bin/sh -
#
# Multi-user mode script
#
# Copyright (c) 2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

umask 022
IFS='
 	'
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin

# X11 database

echo "(Startup) Updating X11 database (X.Org)"

echo "X font index cache, if needed ..."
if type mkfontscale > /dev/null
then
    for directory in /usr/share/fonts/X11/*
    do
        if test ! -e "${directory}/fonts.dir"
        then
            echo "Using \`mkfontscale' for missing fonts.dir at $directory"
            mkfontscale -b -s -l "$directory" && mkfontscale "$directory"
        fi
    done
    unset directory
fi

echo "fontconfig cache: fc-cache --system-only /usr/share/fonts/X11 &"
if type fc-cache > /dev/null
then
    fc-cache --system-only /usr/share/fonts/X11 &
fi

echo "GTK+-2.0 module(s) cache: gtk-query-immodules-2.0 --update-cache &"
if type gtk-query-immodules-2.0 > /dev/null
then
    gtk-query-immodules-2.0 --update-cache &
fi
echo "GTK+-3.0 module(s) cache: gtk-query-immodules-3.0 --update-cache &"
if type gtk-query-immodules-3.0 > /dev/null
then
    gtk-query-immodules-3.0 --update-cache &
fi

echo "GDK pixbuffer loaders: gdk-pixbuf-query-loaders --update-cache &"
if type gdk-pixbuf-query-loaders > /dev/null
then
    gdk-pixbuf-query-loaders --update-cache &
fi

if test -d /usr/lib/gio/modules && type gio-querymodules > /dev/null
then
    echo "GIO module(s) cache: gio-querymodules /usr/lib/gio/modules &"
    gio-querymodules /usr/lib/gio/modules &
fi

echo "GLIB schemas: glib-compile-schemas /usr/local/share/glib* /usr/share/glib*"
if type glib-compile-schemas > /dev/null
then
    for directory in /usr/local/share/glib* /usr/share/glib*
    do
        test -d "$directory" || continue;
        glib-compile-schemas "${directory}"/schemas
    done
    unset directory
fi

echo "Desktop database: update-desktop-database -q &"
if type update-desktop-database > /dev/null
then
    update-desktop-database -q &
fi

# End of X11 database.

# Start services through perpd(8) instances

if type perpboot > /dev/null
then
    # Check for the running udevd(8) inherited from the rc.startup,
    # preventing to kill the udevd instance started by perp
    if type perpok > /dev/null && test ! perpok udevd 2> /dev/null
    then
        # The inherited udevd will be started again to be supervised
        udevadm control --exit ; killall udevd 2> /dev/null
    fi

    echo "(Startup) Monitoring services through perpd(8) instances"
    PERP_BASE=/etc/perp perpboot -d
fi

# Start GPM (General Purpose Mouse daemon)

if test -x /etc/rc.d/rc.gpm
then
    /etc/rc.d/rc.gpm
fi

# Verify the /etc/fstab file to activate the quota system

echo "(Startup) Checking quota availability"
if egrep -q -m 1 '(usr|grp)quota' /etc/fstab
then
    if type quotacheck > /dev/null && type quotaon > /dev/null
    then
        echo "(Startup) Checking /etc/fstab for quota usage"
        quotacheck -vugcm && {
            echo "(Startup) Turn ON quota file system"
            quotaon -vaug
        }
    fi
else
    echo "No quota filesystem declared on /etc/fstab." 1>&2
fi

# Start user-local instructions

if test -x /etc/rc.d/rc.local
then
    echo "(Startup) Running /etc/rc.d/rc.local"
    /etc/rc.d/rc.local
fi

